// pages/admin/User-list/User-list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    users: [
      { id: 1, name: "张三", number: "20201234", gender: "男", major: "2020级软件工程1班", level: "本科", nation: "汉族", place: "四川成都", politics: "共青团员", identity: "123456789012345678", phone: "12345678901" },
      { id: 2, name: "李四", number: "20205678", gender: "女", major: "2020级软件工程2班", level: "专科", nation: "藏族", place: "西藏拉萨", politics: "群众", identity: "876543210987654321", phone: "19876543210" }
    ],
    showForm: false,
    isEdit: false,
    formData: {},
    nextId: 3,
    showDeleteModal: false,
    deleteUserId: null
  },

  // 显示确认删除弹窗
  showDeleteConfirm(e) {
    const id = e.currentTarget.dataset.id;
    this.setData({
      showDeleteModal: true,
      deleteUserId: id
    });
  },

  // 隐藏确认删除弹窗
  hideDeleteConfirm() {
    this.setData({
      showDeleteModal: false,
      deleteUserId: null
    });
  },
  // 确认删除用户
  confirmDelete() {
    const { deleteUserId, users } = this.data;

    if (!deleteUserId) return;

    const updatedUsers = users.filter(user => user.id !== deleteUserId);
    this.setData({
      users: updatedUsers,
      showDeleteModal: false,
      deleteUserId: null
    });

    wx.showToast({
      title: "删除成功",
      icon: "success",
      duration: 2000
    });
  },
   // 显示添加表单
   showAddForm() {
    this.setData({
      showForm: true,
      isEdit: false,
      formData: {}
    });
  },

  // 隐藏表单
  hideForm() {
    this.setData({ showForm: false });
  },

  // 输入框绑定
  onInput(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`formData.${field}`]: value
    });
  },

  // 性别选择
  onGenderChange(e) {
    this.setData({ "formData.gender": e.detail.value });
  },

  // 学习层次选择
  onLevelChange(e) {
    this.setData({ "formData.level": e.detail.value });
  },

  // 保存用户信息
  saveUser() {
    const { formData, isEdit, users } = this.data;

    if (!formData.name || !formData.number || !formData.gender || !formData.major || !formData.level || !formData.nation || !formData.place || !formData.politics || !formData.identity || !formData.phone) {
      wx.showToast({
        title: "请填写所有字段",
        icon: "none",
        duration: 2000
      });
      return;
    }

    if (isEdit) {
      const updatedUsers = users.map(user => user.id === formData.id ? formData : user);
      this.setData({ users: updatedUsers });
    } else {
      const newUser = { ...formData, id: this.data.nextId };
      this.setData({
        users: [...users, newUser],
        nextId: this.data.nextId + 1
      });
    }

    wx.showToast({
      title: isEdit ? "修改成功" : "添加成功",
      icon: "success",
      duration: 2000
    });

    this.hideForm();
  },

  // 编辑用户信息
  editUser(e) {
    const id = e.currentTarget.dataset.id;
    const user = this.data.users.find(user => user.id === id);
    this.setData({
      showForm: true,
      isEdit: true,
      formData: { ...user }
    });
  },

  // 删除用户信息
  deleteUser(e) {
    const id = e.currentTarget.dataset.id;
    const updatedUsers = this.data.users.filter(user => user.id !== id);
    this.setData({ users: updatedUsers });
    wx.showToast({
      title: "删除成功",
      icon: "success",
      duration: 2000
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})